package org.intellij.plugins.junit4.intentions;

import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.apache.log4j.Logger;
import org.intellij.plugins.junit4.JavaMethod;
import org.intellij.plugins.junit4.SelectedFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author tomichj
 */
public class AddTestAnnotationIntention implements IntentionAction
{
    private final Logger log = Logger.getLogger("CONSOLE-WARN");

    @NotNull
    public String getText()
    {
        return "Add @Test annotation";
    }

    @NotNull
    public String getFamilyName()
    {
        return "JUnit.assist";
    }

    public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file)
    {
        JavaMethod method = new SelectedFile(project).currentMethod();
        log.warn("AddTestIntention found method=" + method);
        if (method == null) return false;
        if (!method.isTest()) return false;
        if (method.hasTestAnnotation()) return false;
        return true;
    }

    public void invoke(@NotNull Project project, Editor editor, PsiFile file)
            throws IncorrectOperationException
    {
        JavaMethod method = new SelectedFile(project).currentMethod();
        log.warn("AddTestIntention found method=" + method);
        if (method == null) return;

        method.annotateTest();
    }

    public boolean startInWriteAction()
    {
        return false;
    }
}
